package com.kdg.passwordgenerator;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created with IntelliJ IDEA.
 * User: Adrynalyne
 * Date: 9/14/13
 * Time: 9:49 PM
 * To change this template use File | Settings | File Templates.
 */


@SuppressWarnings({"DefaultFileTemplate", "unchecked"})
class GUIAbout extends JFrame {

    private JButton btnOK;
    private URI uri;
    private URI uri2;
    private JButton website;
    private JButton license;

    public GUIAbout() {
        super("About");
        try {
            this.uri = new URI("http://www.apache.org/licenses/LICENSE-2.0");
            this.uri2 = new URI("http://www.kwestdg.com/");

        } catch (URISyntaxException e) {
            e.getMessage();
        }

        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        int _height = 315;
        int _width = 300;
        this.setMinimumSize(new Dimension(_width, _height));
        this.setResizable(false);

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("KDG_Logo.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public void CreateBody() {
        JPanel mainPanel = new JPanel(new BorderLayout(0,25));
        JPanel bPanel = new JPanel(new BorderLayout());

        btnOK = new JButton("OK");
        btnOK.addActionListener(new EventListener());

        //List of developers.
        String[] dList = {"","","","Robert Bernard","Jeremy Schulte", "Kyle Osendorf"};
        JList devList = new JList(dList);
        devList.setBackground(new Color(0, 0, 0, 0));
        devList.setBorder(BorderFactory.createEmptyBorder());

        //title
        //Date buildDate = new Date();
        //DateFormat dFormat = new SimpleDateFormat("yyyyMdd");


        JLabel titleLabel = new JLabel("KDG Password Generator 1.0.2");
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Font font = new Font("Arial",Font.BOLD,14);
        titleLabel.setFont(font);


        //Dev group image.
        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("KDG_Logo.png"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        JLabel imageLabel = new JLabel(new ImageIcon(image));

        //KDG web link
        website = new JButton ("<HTML><FONT color=\"#000099\"><U>KDG Website</U></FONT></HTML>");
        website.setBorderPainted(false);
        website.setOpaque(true);
        website.setToolTipText(uri2.toString());
        website.addActionListener(new EventListener());
        bPanel.add(website, BorderLayout.CENTER);

        //Apache license link
        license = new JButton("<HTML><FONT color=\"#000099\"><U>Apache License</U></FONT></HTML>");
        license.setBorderPainted(false);
        license.setOpaque(true);
        license.setToolTipText(uri.toString());
        license.addActionListener(new EventListener());
        bPanel.add(license, BorderLayout.WEST);

        bPanel.add(btnOK, BorderLayout.EAST);
        mainPanel.add(titleLabel, BorderLayout.NORTH);
        mainPanel.add(devList, BorderLayout.WEST);
        mainPanel.add(imageLabel, BorderLayout.EAST);
        mainPanel.add(bPanel, BorderLayout.SOUTH);

        this.add(mainPanel);
    }
    private class EventListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnOK) {
                setVisible(false);
            }
            if (e.getSource() == license && Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(uri);
                } catch (Exception a) {
                    JOptionPane.showMessageDialog(rootPane, "Not yet supported.");
                }
            }
            if (e.getSource() == website && Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(uri2);
                } catch (Exception a) {
                    JOptionPane.showMessageDialog(rootPane, "Not yet supported.");
                }
            }
        }
    }
}
